/*
 *  $Id: Hydromancy.cs 1051 2010-05-18 22:10:41Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008-2009 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

namespace Ragima.Cards001.Water.Spells {
    public class Hydromancy: TargetEnchantmentCard<Creature> {
        public Hydromancy(): base("001_w_s_hydromancy") {
            Level = CompleteLevel.ReadyForTest;
            Version = 1;
            Element = ElementType.Water;
            Value = 100;
            Target = CreatureTarget.Both;
            AddManaCost(ElementType.Water, 2);
        }

        protected override void DoAction(TargetArgs<Creature> args) {
            EnchantmentAffect enchantment = new HydromancyAffect(args.Player);
            enchantment.SetData(args.Player, this);
            args.Target.AddAffect(enchantment);
        }

        private class HydromancyAffect: EnchantmentAffect<Hydromancy> {
            private Player _receiver;

            public HydromancyAffect(Player receiver) {
                _receiver = receiver;
            }

            public override Affect Clone() {
                EnchantmentAffect affect = new HydromancyAffect(_receiver);
                affect.SetData(null, Enchantment);
                return affect;
            }

            public override void SetAffect() {
                base.SetAffect();
                GlobalSummary.OnDeath += DoDeath;
                EnemySummary.OnDeath += DoDeath;
            }
            public override void ResetAffect() {
                base.ResetAffect();
                GlobalSummary.OnDeath -= DoDeath;
                EnemySummary.OnDeath -= DoDeath;
            }

            private void DoDeath(AbstractObject obj) {
                if (_receiver == null || obj == Owner) return;
                Creature cr = obj as Creature;
                if (cr != null && cr.Card.Element == ElementType.Water) {
                    _receiver.IncrementMana(ElementType.Water, 1);
                    int heal = cr.Card.GetSumMana() > 5 ? 2 : 1;
                    _receiver.HealBy(NewDamage(DamageType.Health, heal));
                }
            }
        }
    }
}
